package com.category.douyijia.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.category.douyijia.enums.UseingIdent;
import com.category.douyijia.model.base.BaseUser;
import com.jfinal.kit.StrKit;

import sun.util.logging.resources.logging;

/**
 * Use key and value pairs to 
 * store user gender, 
 * login permissions, 
 * user groups, 
 * and user status information. 
 * @author JunfengLiu
 * @version v1.0
 * date time: 2021-7-17 20:17:25 
 */
public class User extends BaseUser<User> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4047114701987162515L;
	/**
	 * it is saved the user sex.
	 */
	/*
	 * 保存性别
	 */
	private static Map<Integer,String> userSexMap;
	/**
	 * it is saved the user login power identity.
	 */
	/*
	 * 保存用户登录身份
	 */
	private static Map<Integer,String> userLoginIdentMap;
	/**
	 * it is saved the user login group.
	 */
	/*
	 * 保存用户组
	 */
	private static Map<Integer,Boolean> userGroupMap;
	/**
	 * it is saved the user login statue.
	 */
	/*
	 * 保存账号状态
	 */
	private static Map<Integer,Boolean> userStatueMap;
	/**
	 * 登录状态
	 */
	private UserLoginState userLoginState = new UserLoginState();
	/**
	 * 保存用户登录状态的Map集合，
	 * 保存用户登录状态编号和登录状态描述
	 */
	private static Map<Integer,String> loginStateMap;
	/**
	 * 添加用户登录状态
	 * @param loginState
	 */
	public void setUserStateByUserId(Integer loginState){
		String userId = this.getUserId();
		if (!StrKit.isBlank(userId)) {
			this.setUserId(this.getUserId());
			if (loginState >= -1 && loginState <= 1) {
				//登录状态合法传值
				userLoginState.setUserLoginState(loginState);
			}else {
				//否则传入0，表示未登录状态
				userLoginState.setUserLoginState(0);
			}
		}
	}
	/**
	 * 查找登录状态
	 * @param userId
	 * @return
	 */
	private int getUserStateByUserId(String userId){
		String id = userLoginState.getUserLoginId();
		if (id.equals(userId)) {
			userLoginState = (UserLoginState)userLoginState
					.dao().find("select * from "
					+ "t_user_login_state "
					+ "where user_login_id = ?", userId);
		}
		if (userLoginState != null) {
			return userLoginState.getUserLoginState();
		}else {
			return 0;
		}
	}
	/**
	 * 内部实现登录效果
	 * @param userId
	 * 传入用户id
	 * @param state
	 * 传入用户登录状态
	 * 
	 */
	public void loginIt(String userId,Integer state,UserLoginState userLoginState){
		int st = 0;
		User user = new User();
		if (!StrKit.isBlank(userId)) {
			user = this.dao().findById(userId);
			st = getUserStateByUserId(userId);
		}
		if (user != null && st != state) {
			userLoginState.setUserLoginState(state);
		}
		if (userLoginState != null) {
			this.userLoginState._setAttrs(userLoginState).save();
		}
	}
	/**
	 * 设置登录状态
	 * @return
	 */
	public static Map<Integer,String> getLoginStateMap(){
		loginStateMap.put(-1, "登录超时");
		loginStateMap.put(0, "未登录");
		loginStateMap.put(1, "已登录");
		return loginStateMap;
	}
	/**
	 * this is save the user sex.
	 * @return
	 * user sex.
	 */
	public static Map<Integer, String> getUserSexMap() {
		userSexMap = new HashMap<Integer, String>();
		userSexMap.put(0, "女");
		userSexMap.put(1, "男");
		return userSexMap;
	}
	/**
	 * this is save the user login
	 * power,and astrict user visit page.
	 * @return
	 * user login power identity.
	 */
	public static Map<Integer, String> getUserLoginIdentMap() {
		userLoginIdentMap = new HashMap<Integer, String>();
		userLoginIdentMap.put(0, UseingIdent.Dev.toString());
		userLoginIdentMap.put(1, UseingIdent.Admin.toString());
		userLoginIdentMap.put(2, UseingIdent.Manager.toString());
		userLoginIdentMap.put(3, UseingIdent.Customer.toString());
		userLoginIdentMap.put(4, UseingIdent.User.toString());
		return userLoginIdentMap;
	}
	/**
	 * this is save the user group statue.
	 * if it is true user have a group.
	 * else the user is a person.
	 * @return
	 * user group statue.
	 */
	public static Map<Integer, Boolean> getUserGroupMap() {
		userGroupMap = new HashMap<Integer, Boolean>();
		userGroupMap.put(0, false);
		userGroupMap.put(1, true);
		return userGroupMap;
	}
	/**
	 * this is save the user statue.
	 * if the statue is true,
	 * the user can login this page,
	 * else the user is not used the login id 
	 * and password login it.
	 * @return
	 * user login statue.
	 */
	public static Map<Integer, Boolean> getUserStatueMap() {
		userStatueMap = new HashMap<Integer, Boolean>();
		userStatueMap.put(0, false);
		userStatueMap.put(1, true);
		return userStatueMap;
	}
	/**
	 * 隐藏关信息
	 * @return
	 * 当前用户信息
	 */
	public User hiddenKey(){
		remove("userPassword");
		return this;
	}
}
